package com.zhangxi.springbootwebzhangxi.controller;

import com.zhangxi.springbootwebzhangxi.pojo.Dept;
import com.zhangxi.springbootwebzhangxi.pojo.Result;
import com.zhangxi.springbootwebzhangxi.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: DeptController
 * Package: com.zhangxi.springbootwebzhangxi.controller
 */
@RestController
@Slf4j
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result depts(){
        log.info("查询部门所有的数据");
        List<Dept> deptList = deptService.allDept();
        return Result.success(deptList);
    }

    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable Integer id){
        //日志记录
        log.info("根据id删除部门");
        deptService.deleteDept(id);
        return Result.success();
    }

    @PostMapping
    public Result insertDept(@RequestBody Dept dept){
        //记录日志
        log.info("新增部门：{}",dept);
        deptService.insertDept(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result GetDept(@PathVariable Integer id){
        //记录日志
        log.info("查询部门：{}",id);
        Dept dept = deptService.getDept(id);
        return Result.success(dept);
    }

    @PutMapping
    public  Result UpdateDept(@RequestBody Dept dept){
        //记录日志
        log.info("修改部门：{}",dept);
        deptService.UpdateDept(dept);
        return Result.success();
    }
}
